local lvgu = fk.CreateSkill{
  name = "xiaobai__lvgu",
}

Fk:loadTranslationTable{
  ["xiaobai__lvgu"] = "履骨",
  [":xiaobai__lvgu"] = "出牌阶段，你可以观看牌堆底一/三张牌，然后选择两项：1.调整手牌数至三/一(每回合各限选一次)；"..
                      "2.可使用其中一张不为【杀】的牌；3.失去1点体力。",
  ["#lvgu_view1"] = "观看牌堆底的一张牌",
  ["#lvgu_view3"] = "观看牌堆底的三张牌",
  ["lvgu_changeHandCard1"] = "将手牌调整至一张",
  ["lvgu_changeHandCard3"] = "将手牌调整至三张",
  ["lvgu_use"] = "可以使用观看的一张牌",
  ["lvgu_loseHp"] = "失去1点体力",
  ["#lvgu_viewAndChoose"] = "履骨",
  ["#lvgu_viewAndChoose&"] = "牌堆底",
  ["#lvgu_Choose"] = "履骨：请选择一项",
  ["@@lvgu_3-turn"] = "履骨 摸至一",
  ["@@lvgu_1-turn"] = "履骨 摸至三",
}

local lvgu_viewAndChoose = fk.CreateSkill{
  name = "#lvgu_viewAndChoose",
}
lvgu_viewAndChoose:addEffect("active", {
  expand_pile = function (self, player)
    return player:getTableMark("lvgu")
  end,
  interaction = function (self, player)
    local mark = player:getTableMark("lvgu_choices")
    return UI.ComboBox{
      choices = mark.choices,
      all_choices = mark.all_choices
    }
  end,
  card_filter = function (self, player, to_select, selected, selected_targets)
    if self.interaction.data == "lvgu_changeHandCard1" then
      if player:getHandcardNum() - #selected > 1 then
        return table.contains(player:getCardIds(Player.Hand), to_select)
      end
    elseif self.interaction.data == "lvgu_changeHandCard3" then
      if player:getHandcardNum() - #selected > 3 then
        return table.contains(player:getCardIds(Player.Hand), to_select)
      end
    elseif self.interaction.data == "lvgu_use" then
      return #selected == 0 and Fk:getCardById(to_select).trueName ~= "slash" and table.contains(player:getTableMark("lvgu"), to_select) and player:canUse(Fk:getCardById(to_select), {bypass_times = true, not_passive = true})
    end
  end,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    if self.interaction.data == "lvgu_use" and #selected_cards == 1 then
      local card = Fk:getCardById(selected_cards[1])
      return card.skill:targetFilter(player, to_select, selected, {}, card, {bypass_times = true})
    end
  end,
  feasible = function (self, player, selected, selected_cards, card)
    if self.interaction.data == "lvgu_changeHandCard1" then
      return #selected_cards >= player:getHandcardNum() - 1
    elseif self.interaction.data == "lvgu_changeHandCard3" then
      return #selected_cards >= player:getHandcardNum() - 3
    elseif self.interaction.data == "lvgu_use" then
      if #selected_cards == 1 then
        local card = Fk:getCardById(selected_cards[1])
        return card.skill:feasible(player, selected, {}, card)
      end
    else
      return true
    end
  end
})

  
lvgu:addEffect("active", {
  anim_type = "control",
  can_use = function (self, player)
    return #Fk:currentRoom().draw_pile > 0
  end,
  interaction = function (self, player)
    local all_choices = {"#lvgu_view1", "#lvgu_view3"}
    local choices = {"#lvgu_view1"}
    if #Fk:currentRoom().draw_pile >= 3 then
      table.insert(choices,"#lvgu_view3")
    end
    return UI.ComboBox{
      choices = choices,
      all_choices = all_choices
    }
  end,
  card_filter = Util.FalseFunc,
  card_num = 0,
  on_use = function (self, room, effect)
    local player = effect.from
    local card_pile = room.draw_pile
    local choice = self.interaction.data
    local all_choices = {
      "lvgu_changeHandCard"..(self.interaction.data == "#lvgu_view1" and 3 or 1 ), "lvgu_use", "lvgu_loseHp",
    }
    local can_choices = {}
    local lvgu_cards = table.slice(card_pile, #card_pile - (self.interaction.data == "#lvgu_view1" and 0 or 2), #card_pile + 1)

    room:setPlayerMark(player,"lvgu", lvgu_cards)

    if player:getMark("@@lvgu_".. (self.interaction.data == "#lvgu_view1" and 3 or 1 ).."-turn") == 0 then
      table.insert(can_choices, all_choices[1])
    end
    if table.find(lvgu_cards, function (cid)
      return player:canUse(Fk:getCardById(cid), {bypass_times = true, not_passive = true})
    end) then
      table.insert(can_choices, all_choices[2])
    end
    table.insert(can_choices, all_choices[3])

    room:setPlayerMark(player,"lvgu_choices",{choices = can_choices, all_choices = all_choices})
    local _, ret = room:askToUseActiveSkill(player,{
      skill_name = "#lvgu_viewAndChoose",
      prompt = "#lvgu_Choose",
      cancelable = false})
    local chosen1 = ret
    table.removeOne(can_choices, chosen1.interaction)
    if #can_choices == 0 then
      if chosen1.interaction == all_choices[1] then
        local num = player:getHandcardNum() - (self.interaction.data == "#lvgu_view1" and 3 or 1)
        if num > 0 then
          room:throwCard(chosen1.cards, lvgu.name, player)
        else
          player:drawCards(-num, lvgu.name)
        end 
        room:setPlayerMark(player,"@@lvgu_".. (self.interaction.data == "#lvgu_view1" and 3 or 1 ).."-turn",1)
      end
      if player.dead then return end

      if chosen1.interaction == all_choices[2] then
        local card = Fk:getCardById(chosen1.cards[1])
        room:useCard{
          card = card,
          from = player,
          tos = chosen1.targets,
          extraUse = true,
        }
      end
      if player.dead then return end


      if chosen1.interaction == all_choices[3]then
        room:loseHp(player, 1, lvgu.name)
      end
      room:setPlayerMark(player, "lvgu_choices")
      room:setPlayerMark(player, "lvgu")
      return
    end
    room:setPlayerMark(player,"lvgu_choices", {choices = can_choices, all_choices = all_choices})
    _,ret = room:askToUseActiveSkill(player,{
      skill_name = "#lvgu_viewAndChoose",
      prompt = "#lvgu_Choose",
      cancelable = false})
    local chosen2 = ret

    if chosen1.interaction == all_choices[1] or chosen2.interaction == all_choices[1] then
      local num = player:getHandcardNum() - (self.interaction.data == "#lvgu_view1" and 3 or 1)
      if chosen1.interaction == all_choices[1] then
        if num > 0 then
          room:throwCard(chosen1.cards, lvgu.name, player)
        else
          player:drawCards(-num, lvgu.name)
        end
      elseif chosen2.interaction == all_choices[1] then
        if num > 0 then
          room:throwCard(chosen2.cards, lvgu.name, player)
        else
          player:drawCards(-num, lvgu.name)
        end
      end
      room:setPlayerMark(player,"@@lvgu_".. (self.interaction.data == "#lvgu_view1" and 3 or 1 ).."-turn",1)
    end
    if player.dead then return end


    if chosen1.interaction == all_choices[2] then
      local card = Fk:getCardById(chosen1.cards[1])
      room:useCard{
        card = card,
        from = player,
        tos = chosen1.targets,
        extraUse = true,
      }
    elseif chosen2.interaction == all_choices[2] then
      local card = Fk:getCardById(chosen2.cards[1])
      room:useCard{
        card = card,
        from = player,
        tos = chosen2.targets,
        extraUse = true,
      }
    end
    if player.dead then return end


    if chosen1.interaction == all_choices[3] or chosen2.interaction == all_choices[3] then
      room:loseHp(player, 1, lvgu.name)
    end
    room:setPlayerMark(player, "lvgu_choices")
    room:setPlayerMark(player, "lvgu")
  end
})

local skels = {}
table.insert(skels, lvgu)
table.insert(skels, lvgu_viewAndChoose)

return skels